<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$return['setting']=$this->plugin_setting('basic');
	$return['list']=array();
	set_html('title','商品核销');
	$mid = $_SESSION['uid'];
	$verifycode = trim($_RQ['verifycode']);
	$id = trim($_RQ['id']);
	if (empty($verifycode)) 
	{
		exi('请输入核销码!','error','jy_weishop/verify.verifygoods/');
	}
	$item = pdo_get('jy_weishop_verifygoods',array('uniacid' => $_SESSION['uniacid'], 'verifycode' => $verifycode));

	if (empty($item)) 
	{
		exi('未查询到记次时商品或核销码已失效,请核对核销码!', 'error','jy_weishop/verify.verifygoods/');
	}
	$saler = pdo_fetch('select * from ' . tablename('jy_weishop_store_worker') . ' where  status=1  and mid=:mid and uniacid=:uniacid limit 1', array(':uniacid' => $_SESSION['uniacid'], ':mid' => $mid));
	$store = pdo_fetch('select * from ' . tablename('jy_weishop_store') . ' where id=:id and uniacid=:uniacid limit 1', array(':id' => $saler['storeid'], ':uniacid' => $_SESSION['uniacid']));
	if (!(empty($item['storeid'])) && !(empty($store)) && ($item['storeid'] != $store['id'])) 
	{
		exi('该商品无法在您所属门店核销!请重新确认!','error','jy_weishop/verify.verifygoods/');
	}
	if (!(empty($item['limitnum']))){
		$verifygoodlogs = pdo_getall('jy_weishop_verifygoods_log',array('verifygoodsid'=>$item['id']));
		$verifynum = 0;
		foreach ($verifygoodlogs as $verifygoodlog ) 
		{
			$verifynum += intval($verifygoodlog['verifynum']);
		}
		$lastverifys = intval($item['limitnum']) - $verifynum;
		if ($lastverifys < 1) 
		{
			exi('核销次数已用完!', 'error','jy_weishop/verify.verifygoods/');
		}
	}
	if (empty($item['limittype'])) {
		$limitdate = intval($item['starttime']) + (intval($item['limitdays']) * 86400);
	}else{
		$limitdate = intval($item['limitdate']);
	}
	if ($limitdate < time()) {
		exi('该商品已过期!', 'error','jy_weishop/verify.verifygoods/');
	}
	$return['post']=array(
		'orderid'=>$item['orderid'],
		'num'=>1,
		'verifycode'=>$verifycode,
	);
	$goods=pdo_get('jy_weishop_goods',array('uniacid' => $_SESSION['uniacid'], 'id' => $item['ordergoodsid']),array('image','title','subtitle'));
	$item=array_merge($item,$goods);
	$termofvalidity = date('Y-m-d H:i', $limitdate);
	$return['item']=$item;
	$return['saler']=$saler;
	$return['store']=$store;
	$return['lastverifys']=$lastverifys;
	$return['termofvalidity']=$termofvalidity;
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$orderid=intval($post['orderid']);
	$num=intval($post['num']);
	$verify_code=trim($post['verifycode']);
	$remarks=trim($post['remarks']);
	$this->model('order')->order_verifygoods($verify_code,$_SESSION['uid'],$num,$remarks);
	exi('核销成功！','','jy_weishop/verify.verifygoods/');
	
}
?>